package com.xyy.saas.payment.callback.core.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.xyy.elasticjob.client.annotation.ElasticJobConf;
import com.xyy.saas.payment.adpater.RouteManager;
import com.xyy.saas.payment.callback.core.result.CallbackRunner;
import com.xyy.saas.payment.common.RocketMqTopic;
import com.xyy.saas.payment.dao.mapper.WithdrawOrderMapper;
import com.xyy.saas.payment.dao.model.WithdrawOrder;
import com.xyy.saas.payment.merchant.core.dto.WithdrawRefundDto;
import com.xyy.saas.payment.merchant.core.dto.WithdrawRefundItem;
import com.xyy.saas.payment.merchant.core.dto.WithdrawRefundVo;
import com.xyy.saas.payment.service.PayAccountService;
import com.xyy.saas.payment.util.DateUtil;
import com.xyy.saas.payment.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 同步提现退汇状态
 */
@Slf4j
@ElasticJobConf(name = "com.xyy.saas.payment.callback.core.job.SyncWithdrawRefundStatusJob", cron = "0 0 0/1 * * ? ")
public class SyncWithdrawRefundStatusJob implements SimpleJob {
    @Resource
    private WithdrawOrderMapper withdrawOrderMapper;
    @Resource
    private RouteManager routeManager;
    @Resource
    private CallbackRunner callbackRunner;

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("SyncWithdrawRefundStatusJob#execute start.");
        try {
            Date endDate = DateUtil.now();
            Date startDate = DateUtil.getOtherDay(endDate, -2);
            WithdrawRefundVo withdrawRefundVo = routeManager.withdrawRefund(WithdrawRefundDto.builder().
                    refundType(WithdrawRefundDto.WITHDRAW).
                    startDate(startDate).
                    endDate(endDate).build());
            log.error("SyncWithdrawRefundStatusJob#execute withdrawRefundVo={}", JSONUtils.toJSON(withdrawRefundVo));

            if(withdrawRefundVo == null || CollectionUtils.isEmpty(withdrawRefundVo.getWithdrawRefundItems())) {
                return;
            }

            List<WithdrawRefundItem> items = withdrawRefundVo.getWithdrawRefundItems();

            for(WithdrawRefundItem item : items) {
                if(StringUtils.isEmpty(item.getBankTradeNo())) {
                    continue;
                }

                WithdrawOrder withdrawOrder = withdrawOrderMapper.selectByChannelWithdrawNo(item.getBankTradeNo());

                if(withdrawOrder == null) {
                    continue;
                }
                // 避免重复通知
                else if(WithdrawOrder.STATUS_REFUND.equals(withdrawOrder.getStatus())) {
                    continue;
                }

                // 1.组装参数
                Map<String, String> noticeParam = new HashMap<>();
                noticeParam.put("status", String.valueOf(WithdrawOrder.STATUS_REFUND));
                noticeParam.put("withdrawNo", item.getWithdrawNo());
                noticeParam.put("errorMsg", item.getRefundReason());

                // 2.发送通知p
                callbackRunner.syncSendMsg(RocketMqTopic.TOPIC_WITHDRAW_NOTICE, noticeParam);
            }
        }
        catch(Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
}
